#include "CPLoaded.hpp"

using namespace BFT::messages;

CPLoaded::CPLoaded(uint32_t _seq, uint32_t _sender)
	: MacArrayMessage::MacArrayMessage(CP_LOADED, computeSize(), _sender, BFT::Parameters::getOrderCount())
{
	seqNo = _seq;
	
	int bytes_size;
	boost::shared_ptr<char> bytes = getBytes(bytes_size);
	
	//write the sequence number
	*(uint32_t*)(bytes.get()+ getOffset()) = htonl(seqNo);
}

CPLoaded::CPLoaded(boost::shared_ptr<char> _bytes, int _bytes_size)
	: MacArrayMessage(_bytes, _bytes_size)
{
	if (getTag() != CP_LOADED)
	{
		fprintf(stderr,"trying to create a CPLoaded from invalid bytes. expected tag : %d, actual tag: %d\n",CP_LOADED, getTag());
		exit(100);
	}
	
	unsigned int offset= getOffset();
	
	//read the sequence number
	seqNo = ntohl( *(uint32_t*)(_bytes.get()+offset) );
	offset += SEQUENCE_NUMBER_SIZE;
	
	if (offset != _bytes_size -getAuthenticationSize())
	{
		fprintf(stderr, "trying to create a CPLoaded from invalid bytes\n");
		exit(101);
	}
}

void CPLoaded::printInfo()
{
	fprintf(stdout,"CPLoaded information\n");
	fprintf(stdout,"seqNo %d\n",seqNo);
}
